package algorithm;

import structure.TreeNode;

/**
 * 打家劫舍|||
 *
 * 相连的两家不能同时被打劫
 * 记录大神的最优题解
 */
public class LeetCode_rob_337 {
    public int rob(TreeNode root) {
        int[] result = robInternal(root);
        return Math.max(result[0], result[1]);
    }

    public int[] robInternal(TreeNode root) {
        if (root == null) {
            return new int[2];
        }
        int[] result = new int[2];
        int[] left = robInternal(root.left);
        int[] right = robInternal(root.right);

        result[1] = root.val + left[0] + right[0];
        result[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        return result;
    }
}
